<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\user;

use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class UserSilence extends PageBase
{

    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();

        $this->objUserAccount = new UserAccount();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'user_id' => 'require',
            'hour' => 'require',
        ];
        $msg = [
            'user_id.require' => '请选择用户',
            'hour.require' => '请选择禁言时长',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
//        $ret = $this->checkToken($param);
//        if (false === $ret) {
//            throw new Exception('check token fail...', Error::ERR_NO_AUTH);
//        }

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        // 2、获取用户信息
        $user_account = $this->objUserAccount->getUserAccount(['user_id' => $param['user_id']]);
        if (false === $user_account) {
            Log::error(__METHOD__ . 'get user account from db fail. [user_id: ' . $param['user_id'] . ']');
            return $this->getRes(Error::ERR_SYS);
        }
        if (empty($user_account)) {
            return $this->getRes(Error::ERR_USER_NOT_EXIST);
        }
//        $user_account = $user_account[0];
//
//        if (($user_account['silence_time'] > 0) && ($param['hour'] > 0)) {
//            return $this->getRes(Error::ERR_USER_IS_SILENCE);
//        }

        $input = [
            'user_id' => $param['user_id'],
            'silence_time' => time() + intval($param['hour']) * 3600
        ];
        $this->objUserAccount->updateUserAccount($input);

        return $this->getRes();

    }
}
